package com.hxd.otc.module.convenience.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.convenience.bean.OrderFinishedBean;
import com.hxd.otc.module.convenience.bean.OrderInfoBean;
import com.hxd.otc.module.convenience.bean.RateBean;
import com.hxd.otc.module.convenience.contract.PayPanelContract;
import com.hxd.otc.module.convenience.model.ConvenienceModel;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.module.wallet.bean.WalletCurrencyBean;
import com.hxd.otc.module.wallet.model.WalletModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lzy on 2018/12/12 0012.
 * description ${支付对话框-支付面板 Presenter}
 */
public class PayPanelPresenter extends BasePresenter<PayPanelContract.IView> implements PayPanelContract.IPresenter {

    private final ConvenienceModel mConvenienceModel;
    private final WalletModel mWalletModel;
    private final UserModel mUserModel;
    private OrderInfoBean mOrderInfoBean;
    private List<WalletCurrencyBean> mWalletCurrencyBeanList;

    private HashMap<String, String> mRateMaps = new HashMap<>();

    public PayPanelPresenter(PayPanelContract.IView iView) {
        super(iView);
        mConvenienceModel = ConvenienceModel.getInstance();
        mWalletModel = WalletModel.getInstance();
        mUserModel = UserModel.getInstance();
    }

    @Override
    public void updateNewPaymentMethod(String newSymbol, String newVolume) {
        mOrderInfoBean.setRealSymbol(newSymbol);
        mOrderInfoBean.setVolume(Double.valueOf(newVolume));
    }

    @Override
    public void initOrderInfo(OrderInfoBean orderInfoBean) {
        mOrderInfoBean = orderInfoBean;
        mRateMaps.clear();
        mRateMaps.put(orderInfoBean.getRealSymbol(), orderInfoBean.getVolume() + "");
    }

    @Override
    public OrderInfoBean getOrderInfoBean() {
        return mOrderInfoBean;
    }

    @Override
    public void pay(String authCode) {
        mOrderInfoBean.setCode(authCode);//设置验证码
        mConvenienceModel.payOrder(mOrderInfoBean, new BaseObserver<OrderFinishedBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull OrderFinishedBean bean) {
                mView.paySuccess(bean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.payFail(errType, errMessage);
            }
        }, mView.<BaseBean<OrderFinishedBean>>bindAutoDispose());
    }

    @Override
    public void requestWalletInfo() {
        mWalletModel.getWalletAccountInfo(new BaseObserver<List<WalletCurrencyBean>>() {
            @Override
            public void onNext(@NonNull String msg, List<WalletCurrencyBean> beans) {
                mWalletCurrencyBeanList = beans;
                mView.getWalletInfoSuccess();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<List<WalletCurrencyBean>>>bindAutoDispose(), false);
    }

    private List<WalletCurrencyBean> getWalletInfos() {
        if (mWalletCurrencyBeanList == null) {
            requestWalletInfo();//重新获取钱包信息
            return new ArrayList<>();
        }
        return mWalletCurrencyBeanList;
    }

    @Override
    public WalletCurrencyBean getCurCoinWalletInfo() {
        List<WalletCurrencyBean> walletInfos = getWalletInfos();
        if (walletInfos == null) return null;
        String realSymbol = getOrderInfoBean().getRealSymbol();
        for (WalletCurrencyBean currencyBean : walletInfos) {
            if (realSymbol.equals(currencyBean.getSymbol())) {
                return currencyBean;
            }
        }
        return null;
    }

    @Override
    public void getRate(String newSymbol) {
        if (mRateMaps.containsKey(newSymbol)) {
            mView.getRateSuccess(newSymbol, mRateMaps.get(newSymbol));
            return;
        }

        mConvenienceModel.getRate(mOrderInfoBean.getSellPrice(), mOrderInfoBean.getPriceUnit(), newSymbol, new BaseObserver<RateBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull RateBean rateBean) {
                mRateMaps.put(rateBean.symbol, rateBean.volume);//进行缓存
                mView.getRateSuccess(rateBean.symbol, rateBean.volume);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<RateBean>>bindAutoDispose());
    }

    @Override
    public void sendPhoneSms() {
        mUserModel.sendExValidTypeMobileSms("ex_mobile", new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.sendCodeSuccess(ResourceUtil.getString(R.string.phone_sms_already_send_to_your_bound_phone));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }

    @Override
    public void sendEmailVerCode() {
        mUserModel.sendExValidTypeEmail("ex_mail", new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.sendCodeSuccess(ResourceUtil.getString(R.string.email_ver_code_already_send_to_your_bound_email));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }
}
